<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品编码" prop="proCode">
        <el-input
          v-model="queryParams.proCode"
          placeholder="请输入商品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="捐赠来源" prop="sourceId">
        <el-select v-model="queryParams.sourceId" filterable placeholder="请选择捐赠来源" style="">
          <el-option
            v-for="item in donate_option"
            :key="item.id"
            :label="item.donateName"
            :value="item.id"
            @keyup.enter.native="handleQuery"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="出入库标志" prop="inventoryFlag">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.inventoryFlag"-->
      <!--          placeholder="请输入出入库标志"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['supermarket:inventory:add']"
        >新增入库
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['supermarket:inventory:edit']"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['supermarket:inventory:remove']"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['supermarket:inventory:export']"-->
      <!--        >导出</el-button>-->
      <!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inventoryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column type="index"></el-table-column>
      <el-table-column label="商品编码" align="center" prop="proCode"/>
      <el-table-column label="商品名称" align="center" prop="productName"/>
      <el-table-column label="规格" align="center" prop="specifications">
        <template slot-scope="scope">
          <span>{{ scope.row.specifications }}  {{ scope.row.color }}  {{ scope.row.size }}</span>
        </template>
      </el-table-column>
      <el-table-column label="数量" align="center" prop="inventory"/>
      <el-table-column label="捐赠来源" align="center" prop="sourceNames">
        <template slot-scope="scope">
          <div v-for="item in scope.row.sourceNames">{{ item }}</div>
        </template>
      </el-table-column>
      <el-table-column label="入库人" align="center" prop="createBy"/>
      <el-table-column label="入库时间" align="center" prop="createTime" width="180"></el-table-column>
      <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
      <!--        <template slot-scope="scope">-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-edit"-->
      <!--            @click="handleUpdate(scope.row)"-->
      <!--            v-hasPermi="['supermarket:inventory:edit']"-->
      <!--          >修改-->
      <!--          </el-button>-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-delete"-->
      <!--            @click="handleDelete(scope.row)"-->
      <!--            v-hasPermi="['supermarket:inventory:remove']"-->
      <!--          >删除-->
      <!--          </el-button>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品库存对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品编码" prop="proCode">
          <el-input v-model="form.proCode" placeholder="请输入商品编码"/>
        </el-form-item>
        <el-form-item label="商品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入商品名称"/>
        </el-form-item>
        <el-form-item label="数量" prop="inventory">
          <!--          <el-input v-model="form.inventory" placeholder="请输入库存"/>-->
          <el-input-number v-model="form.inventory" size="medium" :min="1" label="请输入数量"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="捐赠来源" prop="arDonateSources">
          <el-select
            v-model="form.arDonateSources"
            multiple
            filterable
            allow-create
            default-first-option
            value-key="id"
            placeholder="请选择捐赠来源"
          >
            <el-option
              v-for="item in donate_option"
              :key="item.id"
              :label="item.donateName"
              :value="{ id: item.id }"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <div class="flex-row">
          <el-form-item label="颜色" prop="color" style="width: 48%;margin-right: 30px">
            <el-input v-model="form.color" placeholder="请输入颜色"/>
          </el-form-item>
          <el-form-item label="尺码" prop="size">
            <el-input v-model="form.size" placeholder="请输入尺码"/>
          </el-form-item>
        </div>
        <el-form-item label="规格" prop="specifications">
          <el-input v-model="form.specifications" placeholder="请输入规格"/>
        </el-form-item>
        <!--        <el-form-item label="时间段" prop="timeSlot">-->
        <!--          &lt;!&ndash;          <el-input v-model="form.timeSlot" placeholder="请输入时间段"/>&ndash;&gt;-->
        <!--          <div class="flex-row">-->
        <!--            <el-time-select-->
        <!--              placeholder="起始时间"-->
        <!--              v-model="selectTime.startTime"-->
        <!--              :picker-options="{start: '07:00', step: '00:30',end: '24:00'}"-->
        <!--            >-->
        <!--            </el-time-select>-->
        <!--            <div style="margin: 0 10px;">-</div>-->
        <!--            <el-time-select-->
        <!--              placeholder="结束时间"-->
        <!--              v-model="selectTime.endTime"-->
        <!--              :picker-options="{start: '07:00', step: '00:30',end: '24:00',minTime: selectTime.startTime}"-->
        <!--            >-->
        <!--            </el-time-select>-->
        <!--          </div>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="使用日期" prop="timeInterval">-->
        <!--          &lt;!&ndash;          <el-input v-model="form.timeInterval" placeholder="请输入时间区间"/>&ndash;&gt;-->
        <!--          <el-date-picker clearable-->
        <!--                          v-model="form.timeInterval"-->
        <!--                          type="date"-->
        <!--                          value-format="yyyy-MM-dd"-->
        <!--                          placeholder="请选择使用日期"-->
        <!--          >-->
        <!--          </el-date-picker>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="积分价值" prop="integratedWorth">-->
        <!--          <el-input v-model="form.integratedWorth" placeholder="请输入积分价值"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="所属地区" prop="platformId">-->
        <!--          <el-input v-model="form.platformId" placeholder="请输入所属地区"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="出入库标志" prop="inventoryFlag">-->
        <!--          <el-input v-model="form.inventoryFlag" placeholder="请输入出入库标志"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="出库原因" prop="outboundReason">-->
        <!--          <el-input v-model="form.outboundReason" placeholder="请输入出库原因"/>-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInventory,
  getInventory,
  delInventory,
  addInventory,
  updateInventory,
  addPutInventory
} from '@/api/supermarket/inventory'
import { donateList } from '@/api/donate/source'

export default {
  name: 'Inventory',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品库存表格数据
      inventoryList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        proCode: null,
        productName: null,
        sourceId: null,
        inventoryFlag: 1
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productName: [
          { required: true, message: '请输入商品名称', trigger: 'change' }
        ],
        proCode: [
          { required: true, message: '请上传商品编码', trigger: 'change' }
        ],
        arDonateSources: [
          { type: 'array', required: true, message: '请选择捐赠来源', trigger: 'change' }
        ],
        inventory: [
          { required: true, message: '请输入商品数量', trigger: 'change' }
        ]
      },
      //捐赠来源list
      donate_option: [],
      //时间段
      selectTime: {
        startTime: null,
        endTime: null
      }
    }
  },
  created() {
    this.getList()
    //获取捐赠来源list
    this.getDonateList()
  },
  methods: {
    //获取捐赠来源list
    getDonateList() {
      donateList().then(response => {
        this.donate_option = response
      })
    },
    /** 查询商品库存列表 */
    getList() {
      this.loading = true
      listInventory(this.queryParams).then(response => {
        console.log(response.rows)
        this.inventoryList = response.pageData
        this.total =parseInt(response.total)
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        color: null,
        size: null,
        inventory: null,
        timeSlot: null,
        productId: null,
        specifications: null,
        timeInterval: null,
        integratedWorth: null,
        proCode: null,
        productName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        sourceId: null,
        outboundNumber: null,
        platformId: null,
        inventoryFlag: null,
        outboundReason: null,
        arDonateSources: []
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加商品库存'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getInventory(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改商品库存'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInventory(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            // if (this.selectTime.startTime != null || this.selectTime.endTime != null) {
            //   this.form.timeSlot = this.selectTime.startTime + ' ' + '-' + ' ' + this.selectTime.endTime
            // }
            addPutInventory(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除商品库存编号为"' + ids + '"的数据项？').then(function() {
        return delInventory(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('supermarket/inventory/export', {
        ...this.queryParams
      }, `inventory_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
